<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../layui.css">
</head>
<body>        
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
</fieldset>
<form class="layui-form" action="/admin/user/save" method="post">

  <div class="layui-form-item">
    <div class ="layui-inline">
    <label class="layui-form-label">用户名称</label>
      <div class="layui-input-inline">
        <input type="text" name="tName" required  lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class ="layui-inline">
    <label class="layui-form-label">用户工号</label>
      <div class="layui-input-inline">
        <input type="text" name="tSerial" required  lay-verify="required" autocomplete="off" class="layui-input tSerial">
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">登陆密码</label>
      <div class="layui-input-inline">
        <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input" id="pword1">
      </div>
    </div>
      <div class="layui-inline">
      <label class="layui-form-label">再次确认</label>
      <div class="layui-input-inline">
        <input type="password" required lay-verify="required" placeholder="请再次输入" autocomplete="off" class="layui-input" id="pword2" onchange="check()">
      </div>
      <div class="layui-form-mid layui-word-aux" >
        <span id="tishi"></span>
      </div>
    </div>
  </div>


  <div class = "layui-form-item">
    <div class = "layui-inline">
      <label class = "layui-form-label">联系方式</label>
      <div class= "layui-input-inline">
        <input type="text" name="telephone"  placeholder="请输入工号" autocomplete="off" class ="layui-input">
      </div>
    </div>
    <div class = "layui-inline">
      <label class="layui-form-label">用户角色</label>
      <div class="layui-input-inline">
        <select name="role" lay-verify="required">
          <option value="" selected=""></option>
          <option value="教师">教师</option>
          <option value="学院管理员">学院管理员</option>
          <option value="学校管理员">学校管理员</option>
      </select>
      </div>
    </div>
    </div>
  </div>
  <div class = "layui-form-item">
    <div class="layui-inline">
      <label class = "layui-form-label">所在学院</label>
      <div class = "layui-input-inline">
        <select name ="colleage" lay-verify="required">
          <option value="" selected=""></option>
          <option value="数学与信息学院 软件学院">数学与信息学院 软件学院</option>
          <option value="材料与能源学院">材料与能源学院</option>
          <option value="经济管理学院">经济管理学院</option>
        </select>
      </div>
    </div>
    <div class="layui-inline">
      <label class = "layui-form-label">所在系</label>
      <div class = "layui-input-inline">
        <select name ="department" lay-verify="required">
          <option value="" selected=""></option>
          <option value="管理科学与工程系">管理科学与工程系</option>
          <option value="计算机系">计算机系</option>
          <option value="数学系">数学系</option>
        </select>
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>

<script src="../layui.js" charset="utf-8"></script>

<script src="../jquery.min.js"></script>
<script>

function check(){
      var pwd1 = document.getElementById("pword1").value;
      var pwd2 = document.getElementById("pword2").value;
      if(pwd1 == pwd2) {
                  document.getElementById("tishi").innerHTML="<font color='green'>两次密码相同</font>";
              }
              else {
                  document.getElementById("tishi").innerHTML="<font color='red'>两次密码不相同</font>";
              }
}
$.ajax({
  url:'/admin/user/listUser',
  type:"get",
  dataType:'json',
  data:{},
  success:function(result){
    var max = 0;
    for(var i=0;i<result.length;i++){
      if(result[i].tSerial>max)
        max = result[i].tSerial;
    }
    max = max+1;
    $(".tSerial").val(max);
  },
  error:function(result){
    console.log(result);
  }
})


layui.use(['form', 'layedit', 'laydate','upload'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,upload = layui.upload
  ,laydate = layui.laydate;
  
  var $ =layui.jquery;

  //普通图片上传
  var uploadInst = upload.render({
    elem: '#test1'
    ,url: '/upload/'
    ,before: function(obj){
      //预读本地文件示例，不支持ie8
      obj.preview(function(index, photo, result){
        $('#demo1').attr('src', result); //图片链接（base64）
      });
    }
    ,done: function(res){
      //如果上传失败
      if(res.code > 0){
        return layer.msg('上传失败');
      }
      //上传成功
    }
    ,error: function(){
      //演示失败状态，并实现重传
      var demoText = $('#demoText');
      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
      demoText.find('.demo-reload').on('click', function(){
        uploadInst.upload();
      });
    }
  });










  //日期
  laydate.render({
    elem: '#date'
  });
  //创建一个编辑器
  var editIndex = layedit.build('LAY_demo_editor');
 
  //自定义验证规则

  //以后要留下的验证区域



  form.verify({
    title: function(value){
      if(value.length < 5){
        return '标题至少得5个字符啊';
      }
    }
    ,pass: [/(.+){6,12}$/, '密码必须6到12位']
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });

  
  //监听提交
  
  form.on('submit(formDemo)', function(data){
    console.log(data.field);
    var jhxhr = $.ajax({
      'url': data.form.action,
      'data': data.field,
      'type': 'POST'
    });
    
    jhxhr.done(function(result) {
     alert("添加成功");  
    });

    return false;
  });


});

</script>

</body>
</html>